/**
 * JAVACC DEMO 1.0
 */
package com.ssb.task;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.model.Page;
import com.apache.excp.core.impl.LoadManager;
import com.apache.excp.core.manager.ExportExecl;
import com.apache.excp.core.model.ExportVo;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.portal.common.oscache.CacheHelper;
import com.apache.portal.common.util.PortalPubFactory;
import com.apache.rpc.common.LoadRpcService;
import com.apache.tools.ConfigUtil;
import com.apache.tools.DataMap;
import com.apache.tools.StrUtil;
import com.apache.uct.common.LoginUser;
import com.apache.uct.common.ToolsUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.Callable;

/**
 * description: 导出任务线程类
 *
 * @author qihan 创建时间：2017-9-14
 */
public class StatisticsChannelExcelImpTask implements Callable<Integer> {

    private Map<String, Object> params;
    private Map<String, String> dictMap;
    private String objName;
    private ExportVo vo;

    public StatisticsChannelExcelImpTask(String objName, Map<String,String> dictMap,Map<String, Object> params,ExportVo vo) {
        this.params = params;
        this.objName = objName;
        this.vo = vo;
        this.dictMap = dictMap;
    }

    /**
     * TODO 简单描述该方法的实现功能（可选）.
     *
     * @see Callable#call()
     */
    public Integer call() throws Exception {
        int count = 1;
        try {
            boolean mark = true;
            int j = Integer.parseInt(String.valueOf(params.get("page")));
            List list = new ArrayList();
            String pageName = String.valueOf(params.get("pageName"));
            String formName = String.valueOf(params.get("formName"));
            params.remove("pageName");
            params.remove("formName");
            do {
                ResultEntity entity = IusPluginUtil.getInstance().sendToIusService(pageName, formName, params);

                if (null == entity.getEntity()) {
                    mark = false;
                } else {
                    if (entity.getEntity() instanceof Page) {
                        Page page = (Page) entity.getEntity();
                        if (!Validator.isEmpty(page.getPageObjects())) {
                            list.addAll(page.getPageObjects());
                            if (j >= page.getPageCount()) {
                                mark = false;
                            } else {
                                j++;
                            }
                        } else {
                            mark = false;
                        }
                    } else if(entity.getEntity() instanceof List){
                        list=(List<DataMap>) entity.getEntity();
                        mark = false;
//                        System.out.println(mark);
                    }
                    else{
                        mark = false;
                    }
                }
            } while (mark);
            for (Object obj: list) {
                DataMap dataMap = (DataMap)obj;
                dataMap.put("insur_full_name",Validator.getDefaultStr(dictMap.get(dataMap.get("insurCode")),""));
            }
            ExportExecl exp = LoadManager.getInstance().getExp();
            vo.setParams(objName, list);
            exp.exportData(vo);//downloadFile
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return count;
    }
}
